package com.woniuxy.main.service.imp;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.main.mapper.CoachMapper;
import com.woniuxy.main.mapper.OrderMapper;
import com.woniuxy.main.mapper.StudentMapper;
import com.woniuxy.main.mapper.UserMapper;
import com.woniuxy.main.pojo.Order;
import com.woniuxy.main.pojo.Page;
import com.woniuxy.main.pojo.User;
import com.woniuxy.main.pojo.Vi_or_co_le;
import com.woniuxy.main.pojo.Vi_or_stu_le;
import com.woniuxy.main.service.OrderService;

import lombok.Data;

@Service
@Transactional
@Data
public class OrderServiceImp implements OrderService {

	@Autowired
	private OrderMapper orderMapper;
	@Resource
	private StudentMapper studentMapper;
	@Resource
	private CoachMapper coachMapper;
	@Resource
	private UserMapper userMapper;

	@Override	//学员确认已上完课
	public int confirm(Order order) {
		return orderMapper.confirm(order);
	}

	@Override	//学员确认付款
	public String payment(Order order,String password) {
		String result = "付款失败！";
		//根据oid获取用户信息
		User sUser = studentMapper.getUser(order.getOid());
		//验证用户密码是否正确
		if(!sUser.getPayPwd().equals(password)){
			result = "支付密码错误！";
			return result;
		}
		//根据oid获取用户信息
		User cUser = coachMapper.getUser(order.getOid());
		//根据oid获取教练收费
		double price = coachMapper.coachPrice(order.getOid());
		//检查学员账户余额
		double money = userMapper.balanceCheck(sUser.getUid());
		if(money < price){
			result = "账户余额不足，请先充值！";
			return result;
		}
		//进行付款
		sUser.setMoney(sUser.getMoney().subtract(new BigDecimal(price+"")));
		cUser.setMoney(cUser.getMoney().add(new BigDecimal(price+"")));
		//将用户余额更新到数据库
		int sRow = userMapper.updateMoney(sUser);
		int cRow = userMapper.updateMoney(cUser);
		if(sRow > 0 && cRow > 0){
			//更新订单信息
			order.setStatus(2);
			order.setUpdateDate(new SimpleDateFormat("y-M-d HH:mm:ss").format(new Date()));
			int row = orderMapper.updateOrder(order);
			if(row > 0){
				result = "付款成功！";
			}
		}
		return result;
	}
	@Override	//教练确认付款
	public String payment2(Order order,String password) {
		String result = "退款失败！";
		//根据oid获取用户信息
		User cUser = coachMapper.getUser(order.getOid());
		//验证用户密码是否正确
		if(!cUser.getPayPwd().equals(password)){
			result = "支付密码错误！";
			return result;
		}
		//根据oid获取用户信息
		User sUser = studentMapper.getUser(order.getOid());
		//根据oid获取教练收费
		double price = coachMapper.coachPrice(order.getOid());
		//检查教练账户余额
		double money = userMapper.balanceCheck(cUser.getUid());
		if(money < price){
			result = "账户余额不足，请先充值！";
			return result;
		}
		//进行付款
		cUser.setMoney(cUser.getMoney().subtract(new BigDecimal(price+"")));
		sUser.setMoney(sUser.getMoney().add(new BigDecimal(price+"")));
		//将用户余额更新到数据库
		int cRow = userMapper.updateMoney(cUser);
		int sRow = userMapper.updateMoney(sUser);
		if(cRow > 0 && sRow > 0){
			//更新订单信息
			order.setStatus(4);
			order.setUpdateDate(new SimpleDateFormat("y-M-d HH:mm:ss").format(new Date()));
			int row = orderMapper.updateOrder(order);
			if(row > 0){
				result = "退款成功！";
			}
		}
		return result;
	}

	@Override	//教练接受学员签约，新增一条订单信息
	public int insertOrder(Order order) {
		return orderMapper.insertOrder(order);
	}

	@Override	//分页显示订单
	public Page<Vi_or_co_le> studentOrder(Page page, int status,int uid) {
		//根据uid获取sid
		int sid = studentMapper.findSidByUid(uid);
		//获取订单条数
		int total = orderMapper.totalOrder(status,sid);
		//将两个参数封装到map集合
		Map<String,Object> map = new HashMap<>();
		map.put("page", page);
		map.put("status", status);
		map.put("sid", sid);
		//获取当前页订单信息
		List<Vi_or_co_le> orders = orderMapper.studentOrder(map);
		//将数据封装到Page
		page.setTotal(total);
		page.setData(orders);
		return page;
	}

	@Override	//更改订单		//教练也用这个
	public int updateOrder(Order order) {
		return orderMapper.updateOrder(order);
	}

	@Override	//删除订单
	public int deleteOrder(Order order) {
		return orderMapper.deleteOrder(order);
	}

	@Override	//分页显示订单---用于教练
	public Page<Vi_or_stu_le> coachOrder(Page page, int status, int uid) {
		//根据uid获取cid
		int cid = coachMapper.findCidByUid(uid);
		//获取订单条数
		int total = orderMapper.totalOrder_coach(status,uid);
		//将两个参数封装到map集合
		Map<String,Object> map = new HashMap<>();
		map.put("page", page);
		map.put("status", status);
		map.put("cid", cid);
		//获取当前页订单信息
		List<Vi_or_stu_le> orders = orderMapper.coachOrder(map);
		//将数据封装到Page
		page.setTotal(total);
		page.setData(orders);
		return page;
	}
}
